package com.ocean.linkedlist;

import java.util.HashSet;
import java.util.Set;

/**
 * https://leetcode.cn/problems/linked-list-cycle/
 */
public class LinkedListCycle2 {
    public static void main(String[] args) {

        ListNode e5 = new ListNode(5, null);
        ListNode e4 = new ListNode(4, e5);
        ListNode e3 = new ListNode(3, e4);
        ListNode e2 = new ListNode(2, e3);
        ListNode e1 = new ListNode(1, e2);
        e5.next = e1;
        boolean b = new LinkedListCycle2().hasCycle(e1);
        System.out.println(b);
    }


    public boolean hasCycle3(ListNode head) {
        return false;
    }

    public boolean hasCycle2(ListNode head) {
        return false;
    }

    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (slow == fast) {
                return true;
            }

        }
        return false;
    }
}
